﻿using CommonLib;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YouErYuanModels;

namespace YouErYuanBLL
{
    public class CourseBLL : IDisposable
    {
        /// <summary>
        /// 分页查询幼儿园班级记录
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="youErYuanName">幼儿园班级名称</param>
        /// <param name="total">条件查询总记录数</param>
        /// <returns></returns>
        public object CourseSettingPageList(string date, int pageIndex, int pageSize, string youErYuanName, out int total)
        {
            //List<School> list = new List<School>();
            string where = "";
            if (string.IsNullOrEmpty(youErYuanName) == false)
            {
                youErYuanName = SqlFilter.Filter(youErYuanName);
                where = " schoolid = '" + youErYuanName + "'";
                if (string.Equals("this", date))
                    where += " and DATEDIFF(WK,startdate,GETDATE())=0";
                else if (string.Equals("last", date))
                    where += " and DATEDIFF(WK,startdate,GETDATE())=1";
                else if (string.Equals("next", date))
                    where += " and DATEDIFF(WK,startdate,GETDATE())=-1";
            }
            DataTable dt = SQLHepler.GetPaging("[startDate] desc", "[T_Course]", where, pageIndex, pageSize, out total);
            return dt;
        }

        /// <summary>
        /// 删除课程
        /// </summary>
        /// <param name="course"></param>
        /// <returns></returns>
        public object CourseDel(Course course)
        {
            string sql = "delete T_Course where id=@id";
            SqlParameter[] par = new SqlParameter[] {
                new SqlParameter("@id",course.id)
            };
            return SQLHepler.ExecuteSql(sql, par);
        }

        /// <summary>
        /// 科目数据
        /// </summary>
        /// <returns></returns>
        public object SubjectList()
        {
            string sql = "SELECT [id],[pid],[name],[type] FROM [dbo].[T_Dictionary] where type='course' and pid!=''";
            DataTable dt = SQLHepler.GetDataTable(sql, null);
            return dt;
        }
        /// <summary>
        /// 更新科目数据
        /// </summary>
        /// <returns></returns>
        public object InsertOrUpdate(string type, Course course)
        {
            string sql = "";
            //验证是否重复语句
            string sqls = "select top 2 id,type,startDate  from t_course where startdate=@startDate and type=@type";
            //插入语句
            string sqlA = "insert into T_Course values (newid(),@CourseName,@type,@startDate,@schollId)";
            //修改语句
            string sqlB = "update T_Course set CourseName=@CourseName,type=@type,startDate=@startDate where id=@id";
            SqlParameter[] parameter = new SqlParameter[]{
                    new SqlParameter("@CourseName", course.courseName+""),
                    new SqlParameter("@type", course.type+""),
                    new SqlParameter("@startDate", course.startDate+""),
                    new SqlParameter("@Id", course.id+""),
                    new SqlParameter("@schollId", course.schoolId+"")};
            sql = type == "add" ? sqlA : sqlB;
            //验证是否重复
            DataTable dt = SQLHepler.GetDataTable(sqls, parameter);
            if (dt!=null && dt.Rows.Count > 0)
            {
                bool isTypes = dt.Rows[0]["type"] + "" == course.type && Convert.ToDateTime(dt.Rows[0]["startDate"]) == course.startDate;
                bool isID = dt.Rows[0]["id"]+"" == course.id;
                //验证需要修改的记录是否已存在
                if (type == "add" || !isID && isTypes)
                {
                    return 3;
                }
                
            }
            int count = SQLHepler.ExecuteSql(sql, parameter);
            return count;
        }
        public object Query(string id)
        {
            string sql = "select * from T_Course where id=@id";
            DataTable dt = SQLHepler.GetDataTable(sql, new SqlParameter("@id", id));
            return dt;
        }
        /// <summary>
        /// 复制本周课程到下周,先删除下周课程，在插入数据
        /// </summary>
        /// <returns></returns>
        public object Cpoy()
        {
            string sql = "delete dbo.T_Course where DATEDIFF(WK,startdate,GETDATE())=-1";
            int count = SQLHepler.ExecuteSql(sql);
            if (count < 0)
            {
                return 0;
            }
            sql = @"insert into T_Course select newid(),courseName,type,dateadd(dd,7,startdate),schoolId
                            from dbo.T_Course where DATEDIFF(WK, startdate, GETDATE()) = 0";
            return SQLHepler.ExecuteSql(sql);
        }
        /// <summary>
        /// 新增科目
        /// </summary>
        /// <param name="subjectName"></param>
        /// <returns></returns>
        public object InsertSuject(string subjectName)
        {
            string sql = string.Format("insert into t_KeMu values (newid(),'{0}',0)", subjectName);
            int count = SQLHepler.ExecuteSql(sql, null);
            if (count > 0)
                return 1;
            else
                return 0;
        }
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}
